package tk.mybatis.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.springboot.mapper.ResourceMapper;
import tk.mybatis.springboot.model.Dept;
import tk.mybatis.springboot.model.Message;
import tk.mybatis.springboot.model.Resource;

import java.util.List;

/**
 * Created by  on 2017/4/12.
 */
@RestController
@RequestMapping("/res")
@Transactional
public class ResourceController {

    @Autowired
    private ResourceMapper resourceMapper;

    @RequestMapping("/add")
    public Object add(Resource obj){
         resourceMapper.insertSelective(obj);
        return new Message();
    }
    @RequestMapping("")
    public Object list(Model model){
        List<Resource> resources = resourceMapper.selectAll();
        for(Resource resource:resources){
            resource.setAttach(null);
        }
        model.addAttribute("list",resources);
        return new Message(resources);
    }
    @RequestMapping("/delete/{id}")
    public Object delete(@PathVariable Integer id){
         resourceMapper.deleteByPrimaryKey(id);
        return new Message();
    }


}
